
package protocol.transactions;

import org.json.JSONException;
import org.json.JSONObject;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;

import com.netease.http.THttpRequest;
import com.netease.pkgRelated.ErrorToString;
import com.netease.task.example.StringAsyncTransaction;

public class PostLogTransaction extends StringAsyncTransaction {

    public PostLogTransaction() {
        super(XoneBaseTransaction.TRANSACTION_TYPE_POSTLOG);
    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createPostLogReqeust();
        sendRequest(request);
    }

    @Override
    protected void onTransactionError(int errCode, Object obj) {
        notifyError(errCode, obj);

    }

    @Override
    protected void onTransactionSuccess(int code, Object obj) {
//        NTLog.e("PostLogTransaction", "obj is" + obj);
        boolean result = false;
        if (obj != null && obj instanceof String) {
            JSONObject json = null;
            try {
                json = new JSONObject((String)obj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (json != null) {
                boolean success = json.optBoolean("success");
                if (success) {
                    String fileId = json.optString("fileId");
                    result = true;
                    notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, fileId);

                }
            }
        }
        if (!result) {
            notifyError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION,
                    ErrorToString.getString(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION));
        }
    }

}
